﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Objects;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Data.Entity;
using HotellApplikationASP;

namespace AnnuleringsCheck
{
    class dbPolling
    {
        public void Run()
        {
            DateTime? time = DateTime.Today.AddDays(10);
            var db = new HotellbokningASPEntities();
            var unpaid = db.Orders.Where(r => r.OrderDate > time && r.IsPayed == false);

            if (unpaid.Any())
            {
                foreach (var p in unpaid)
                {
                    var email = db.AspNetUsers.Where(r => r.Email == p.AspNetUser.Email).Select(c => c.Email).FirstOrDefault();
                    var details = db.Orders.Where(r => r.Id == p.Id).Select(c => c.Id).FirstOrDefault();

                  
                            var mail = new MailClient();
                            mail.MyMail(email, "Cancelled booking", "Booking no. " + details.ToString() + "removed");
                   
                }
                Console.WriteLine(@"Removed " + unpaid.Count() + " lines");
                db.Orders.RemoveRange(unpaid);
                db.SaveChanges();
                
            }

            

        }
    }
}
